package com.robotic.web.controller.udp.controller;


import com.alibaba.fastjson2.JSONObject;
import com.robotic.common.core.domain.AjaxResult;
import com.robotic.common.utils.StringUtils;
import com.robotic.tcp.RobotMonitor;
import com.robotic.udp.ExecuteNavigation;
import com.robotic.udp.SpeedSourceSwitcher;
import com.robotic.udp.UnifiedUDPReceiver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/execute")
public class ExecuteNavigationController {

    @Autowired
    private ExecuteNavigation executeNavigation;

    @Autowired
    private SpeedSourceSwitcher sourceSwitcher;


    @GetMapping("/lines")
    public AjaxResult getLines()
    {
        List<String> lines = executeNavigation.getLines();
        return AjaxResult.success(lines);
    }

    @GetMapping("/player/{fileAbsPath}")
    public AjaxResult executePlayer(@PathVariable("fileAbsPath") String fileAbsPath)
    {
        if (StringUtils.isEmpty(fileAbsPath)) {
            return AjaxResult.error("请选择线路");
        }
        JSONObject lastBattery = UnifiedUDPReceiver.lastBattery;
        if (lastBattery != null) {
            int currentData = Integer.parseInt(lastBattery.get("currentData").toString());
            if (currentData < 0) {
                return AjaxResult.success("请将机器人移到充电桩再执行", -1);
            }
        }
        executeNavigation.executePlayer(fileAbsPath);
        return AjaxResult.success(1);
    }


    @GetMapping("/player/xyList")
    public AjaxResult xyList()
    {
        return AjaxResult.success(RobotMonitor.xyList);
    }


    @GetMapping("/pauseNavigation")
    public AjaxResult pauseNavigation()
    {
        sourceSwitcher.setSpeedSource(SpeedSourceSwitcher.SpeedSource.JOYSTICK);
        //pauseNavigation.toggleMotionState();
        return AjaxResult.success();
    }


    @GetMapping("/keepOn")
    public AjaxResult keepOn()
    {
        sourceSwitcher.setSpeedSource(SpeedSourceSwitcher.SpeedSource.NAVIGATION_MODULE);
        //pauseNavigation.toggleMotionState();
        return AjaxResult.success();
    }


}
